/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package com.by.server.impl;

import com.by.common.utils.AutoCode;
import com.by.common.utils.AutoData;
import com.by.mapper.PurReturnDetailsMapper;
import com.by.mapper.PurReturnMapper;
import com.by.pojo.PurReturn;
import com.by.server.PurReturnServer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>Project: erp-cloud-api - PurReturnServerImpl</p>
 * <p>Powered by yzc On 2024-01-18 14:23:28</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Service
public class PurReturnServerImpl implements PurReturnServer {

    @Autowired
    PurReturnMapper purReturnMapper;

    @Autowired
    PurReturnDetailsMapper purReturnDetailsMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public int save(PurReturn purReturn) {
        String code = AutoCode.autoCodePrefix("退货")+ AutoData.getNowData();
        String maxCode = purReturnMapper.maxCode(code);
        String newCode = AutoCode.getNewCode(maxCode,code);
        purReturn.setCode(newCode);
        purReturn.getPurReturnDetailsList().forEach(purReturnDetails -> {
            purReturnDetails.setReturnCode(newCode);
            purReturnDetailsMapper.insert(purReturnDetails);
        });
        int rs = purReturnMapper.insert(purReturn);
        if (rs > 0){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("supCode",purReturn.getSupCode());
            hashMap.put("returnCode",newCode);
            hashMap.put("subject",purReturn.getSubject());

            rabbitTemplate.convertAndSend("purReturnExchangeRe","purReturn",hashMap);
        }


        return rs;
    }
}
